<?php

class Admin_Model_User_Mapper extends Admin_Model_MapperAbstract {

	public function __construct($db) {
		parent::__construct($db);
		
		$this->setDbTable(new Admin_Model_User_DbTable(array(Admin_Model_User_DbTable::ADAPTER => $db)));
	}
	/**
	 * 
	 * @param Admin_Model_User $model
	 * @return type
	 */
	public function save(Admin_Model_User_User $model) {
		$Data = array(
			'ID' => $model->getId(),
			'UserName' => $model->getUserName(),
			'Password' => $model->getPassword(),
			'Created' => $model->getCreated(),
			'Status' => $model->getStatus(),
			'Email' => $model->getEmail(),
			'GroupID' => $model->getGroupId(),
			'Meta' => $model->getMeta()
		);

		return parent::save($Data);
	}

	/**
	 * 
	 * @param	int	$id
	 * @return \Admin_Model_User
	 */
	public function find($id) {
		$Result = parent::find($id);
		if (null === $Result) {
			return;
		}

		$User = new Admin_Model_User_User();
		$User->setId($Result->ID)
				->setUserName($Result->UserName)
				->setEmail($Result->email)
				->setPassword($Result->Password)
				->setMeta($Result->Meta)
				->setGroupId($Result->GroupID)
				->setStatus($Result->Status)
				->setCreated($Result->Created);

		return $User;
	}
	
	/**
	 * 
	 * @return array
	 */
	public function fetchAll($where = null, $order = null, $count = null, $offset = null) {
		$Rows = parent::fetchAll($where, $order, $count, $offset);
		$Users = array();
		foreach ($Rows as $Row) {
			$User = new Admin_Model_User();
			$User->setId($Row->ID)
				->setUserName($Row->UserName)
				->setEmail($Row->email)
				->setPassword($Row->Password)
				->setMeta($Row->Meta)
				->setGroupId($Row->GroupID)
				->setStatus($Row->Status)
				->setCreated($Row->Created);
		}
		
		return $Users;
	}

}
